import {Context, Next} from 'koa';
import {WrapperResponse} from '@/utils/wrapperResponse';

const koaJwtMiddleware = () => (ctx: Context, next: Next) => {
    return next().catch(err => {
        if (401 == err.status) {
            ctx.status = 401;
            ctx.body = WrapperResponse.noAuth('Protected resource, use Authorization header to get access');
        } else {
            throw err;
        }
    });
};

export default koaJwtMiddleware;
